/*******************************************************************************
 *        Module: rendering
 *          File: EGLMacro.h
 * Functionality: 
 *       Related:
 *        System: Android
 *      Language: C++
 *        Author: xuefeng.huang
 *        E-mail: xuefeng.huang@xjsd.com
 *       Version: V1.0 
 * Copyright (c) 2023 upuphone Technologies, Inc. 
 * All Rights Reserved.
 * Confidential and Proprietary - upuphone Technologies, Inc.
 * -----------------------------------------------------------------------------
 * Revisions:
 * Date        Version     Reviser       Description
 * 2023-04-12  v1.0        xuefeng.huang  created
 ******************************************************************************/

#ifndef XR_ENGINE_RENDERING_EGL_EGL_MACRO_H
#define XR_ENGINE_RENDERING_EGL_EGL_MACRO_H

#include <EGL/egl.h>
#include "AutoLog.h"

#define EGL_INVALID_CONFIG_VALUE 0x10000000

#ifndef LOG_EGL_ENABLE
#define LOG_EGL_ENABLE 1
#endif

#define COMBINE_ATTRIBUTE ""
#define COMBINE_CONFIG_ATTRIBUTE ""
#define CHECK_EGL_INVOKE(fun) (bool(true))
#define EGL_INVOKE(fun) (fun,(bool(true)))

#if LOG_EGL_ENABLE

#undef COMBINE_ATTRIBUTE
#define COMBINE_ATTRIBUTE(type, name)                       \
({                                                          \
    char temp[64]={0};                                      \
    EGLint value;                                           \
    eglQuery##type(eglDisplay, type, name, &value);         \
    snprintf(temp, 64, #name " = %d\n", value);             \
    temp;                                                   \
})

#undef COMBINE_CONFIG_ATTRIBUTE
#define COMBINE_CONFIG_ATTRIBUTE(type, name)                \
({                                                          \
    char temp[64]={0};                                      \
    EGLint value;                                           \
    eglGet##type##Attrib(eglDisplay, type, name, &value);   \
    snprintf(temp, 64, #name " = %d\n", value);             \
    temp;                                                   \
})


#undef CHECK_EGL_INVOKE
#define CHECK_EGL_INVOKE(fun)                                   \
({                                                              \
    bool egl_invoke;                                            \
    EGLint egl_error_code = eglGetError();                      \
    egl_invoke = (egl_error_code == EGL_SUCCESS);               \
    if (!egl_invoke) {                                          \
        LOGE(#fun" failed error code: %u", egl_error_code);     \
    }                                                           \
    egl_invoke;                                                 \
})


#undef EGL_INVOKE
#define EGL_INVOKE(fun)                                         \
({                                                              \
    bool egl_invoke;                                            \
    fun;                                                        \
    EGLint egl_error_code = eglGetError();                      \
    egl_invoke = (egl_error_code == EGL_SUCCESS);               \
    if (!egl_invoke) {                                          \
        LOGE(#fun" failed error code: %u", egl_error_code);     \
    }                                                           \
    egl_invoke;                                                 \
})

#endif

#endif // XR_ENGINE_RENDERING_EGL_EGL_MACRO_H
